package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.ArticleBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.JustifyTextView;
import com.yao.sdk.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenjiezeng on 2017/11/8.
 */
public class ArticleListAdapter extends RecyclerView.Adapter {
    public final static int TYPE_HEADER = 0;
    public final static int TYPE_ONE = 1;
    public final static int TYPE_TWO = 2;
    public final static int TYPE_THREE = 3;
    private View mHeaderView;
    private List<ArticleBean> articleList=new ArrayList<>();
    private Context mContext;
    private OnArticleItemClickListener mListener;
    public ArticleListAdapter(Context context){
        this.mContext=context;
    }
    public void setData(List<ArticleBean> list) {
        this.articleList = list;

    }
    public void setOnInfoItemClickListener(OnArticleItemClickListener listener){
        this.mListener=listener;
    }
    public void addHead(View v){
        this.mHeaderView=v;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        if (viewType == TYPE_HEADER && mHeaderView != null) {
            v = mHeaderView;
            return new HeadViewHolder(v);
        }else if (viewType==TYPE_ONE){
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.information_item_one, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderOne(v);
        }else if (viewType==TYPE_TWO){
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.information_item_two, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderTwo(v);
        }else {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.information_item_three, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderThree(v);
        }


    }
    public  class HeadViewHolder extends RecyclerView.ViewHolder {

        public HeadViewHolder(View v) {
            super(v);
        }
    }
    public class ViewHolderOne extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title;
        private TextView browse,date;
        private ImageView img;
        private RelativeLayout mainLay;
        public ViewHolderOne(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            browse=(TextView)v.findViewById(R.id.browse);
            date=(TextView)v.findViewById(R.id.date);
            img=(ImageView)v.findViewById(R.id.img);
            mainLay=(RelativeLayout)v.findViewById(R.id.main_lay);
        }
    }
    public class ViewHolderTwo extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title;
        private TextView browse,date;
        private ImageView img;
        private int imgHeight=0;
        private LinearLayout mainLay;
        public ViewHolderTwo(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            browse=(TextView)v.findViewById(R.id.browse);
            date=(TextView)v.findViewById(R.id.date);
            img=(ImageView)v.findViewById(R.id.img);
            imgHeight= (int)((CommonUtil.getScreenWidth(mContext)-CommonUtil.dip2px(mContext,24))/1.8);
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,imgHeight);
            lp.setMargins(CommonUtil.dip2px(mContext,12),0,CommonUtil.dip2px(mContext,12),0);
            img.setLayoutParams(lp);
            mainLay=(LinearLayout) v.findViewById(R.id.main_lay);
        }
    }
    public class ViewHolderThree extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title;
        private TextView browse,date;
        private RelativeLayout mainLay;
        public ViewHolderThree(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            browse=(TextView)v.findViewById(R.id.browse);
            date=(TextView)v.findViewById(R.id.date);
            mainLay=(RelativeLayout)v.findViewById(R.id.main_lay);
        }
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            return;
        }else if (getItemViewType(position) == TYPE_ONE){
            if (mHeaderView!=null)
                position--;
            ((ViewHolderOne)holder).title.setText(articleList.get(position).getTitle());
            ((ViewHolderOne)holder).date.setText(articleList.get(position).getRelease_time());
            ((ViewHolderOne)holder).browse.setText(CommonUtil.numConvert(Integer.valueOf(articleList.get(position).getBrowse()),1)+"阅读");
            GlideUtil.getInstance().load(mContext,articleList.get(position).getMaster_url(),((ViewHolderOne)holder).img,true);
            final int finalPosition = position;
            ((ViewHolderOne)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onArticleClick(articleList.get(finalPosition));
                    }
                }
            });
        }else if (getItemViewType(position) == TYPE_TWO){
            if (mHeaderView!=null)
                position--;
            ((ViewHolderTwo)holder).title.setText(articleList.get(position).getTitle());
            ((ViewHolderTwo)holder).date.setText(articleList.get(position).getRelease_time());
            ((ViewHolderTwo)holder).browse.setText(CommonUtil.numConvert(Integer.valueOf(articleList.get(position).getBrowse()),1)+"阅读");
            GlideUtil.getInstance().load(mContext,articleList.get(position).getMaster_url(),((ViewHolderTwo)holder).img,true);
            final int finalPosition = position;
            ((ViewHolderTwo)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onArticleClick(articleList.get(finalPosition));
                    }
                }
            });
        }else {
            if (mHeaderView!=null)
                position--;
            ((ViewHolderThree)holder).title.setText(articleList.get(position).getTitle());
            ((ViewHolderThree)holder).date.setText(articleList.get(position).getRelease_time());
            ((ViewHolderThree)holder).browse.setText(CommonUtil.numConvert(Integer.valueOf(articleList.get(position).getBrowse()),1)+"阅读");
            final int finalPosition = position;
            ((ViewHolderThree)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onArticleClick(articleList.get(finalPosition));
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mHeaderView != null ? articleList.size() + 1 : articleList.size();
    }
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView != null){
            if (position==0){
                return TYPE_HEADER;
            }else {
                if (articleList.get(position-1).getMaster_url()==null||articleList.get(position-1).getMaster_url().equals("")){
                    return TYPE_THREE;
                }else {
                    if (articleList.get(position-1).getIs_special().equals("1")){
                        return TYPE_ONE;
                    }else {
                        return TYPE_TWO;
                    }
                }

            }

        }else {
            if (articleList.get(position).getMaster_url()==null||articleList.get(position).getMaster_url().equals("")){
                return TYPE_THREE;
            }else {
                if (articleList.get(position).getIs_special().equals("1")){
                    return TYPE_ONE;
                }else {
                    return TYPE_TWO;
                }
            }

        }

    }

    public interface OnArticleItemClickListener {
        void onArticleClick(ArticleBean bean);
    }
}
